package id.net.iconpln.bb.server.dao;

import id.net.iconpln.bb.server.util.CommonModule;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import oracle.jdbc.OracleTypes;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StockDao {
@Autowired	private SessionFactory sessionFactory;
	
	public List<Map> getListStock(String kode, Map map){
		List<Map> lMap = new ArrayList<Map>();
		Session session = sessionFactory.getCurrentSession();
        Connection con = session.connection();
        try {
            String sql = null;
            CallableStatement call;            
            
            System.out.println("kode :" + kode.toString());
            
            sql = "{call BBTRXGUDANG.SETID_PLTU(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_pltu").toString());
            call.execute();	 	
            
            sql = "{? = call BBTRXGUDANG.GETLISTGUDANGBY(?)}";
            call = con.prepareCall(sql);
            call.registerOutParameter(1, OracleTypes.CURSOR);
            call.setString(2, kode);
            call.execute();
            
            ResultSet rset = (ResultSet) call.getObject(1);
            lMap = CommonModule.convertResultsetToListLower(rset);
        }catch (Exception e) {
			e.printStackTrace();
			map.clear();
            map.put("message", e.getMessage());  

		}
        
        return lMap;
	}
	
	public List<Map> insertStock(String kode, Map map){
		List<Map> lMap = new ArrayList<Map>();
		Session session = sessionFactory.getCurrentSession();
        Connection con = session.connection();
        try {
            String sql = null;
            CallableStatement call;

            sql = "{call BBTRXGUDANG.SETVOLUME_AWAL(?)}";
            call = con.prepareCall(sql);
            call.setDouble(1, Double.parseDouble(map.get("volume_awal").toString()));
            call.execute();	 	

            sql = "{call BBTRXGUDANG.SETINDEPENDEN_SURVEYOR(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("independen_surveyor").toString());
            call.execute();	 	
            
            sql = "{call BBTRXGUDANG.SETVOLUME_OPNAME(?)}";
            call = con.prepareCall(sql);
            call.setDouble(1, Double.parseDouble(map.get("volume_opname").toString()));
            call.execute();	 	
            
            sql = "{call BBTRXGUDANG.SETNO_SURAT(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("no_surat").toString());
            call.execute();	 	

            sql = "{call BBTRXGUDANG.SETTANGGAL(?)}";
            call = con.prepareCall(sql);
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            Date tgl = df.parse(map.get("tanggal").toString());
            call.setDate(1, new java.sql.Date(tgl.getTime()));
            call.execute();	 	
    		
            sql = "{call BBTRXGUDANG.SETAPP_ACCESS(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("app_access").toString());
            call.execute();
            
            
            sql = "{call BBTRXGUDANG.SETID_PLTU(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("id_pltu").toString());
            call.execute();	 	
    		
            sql = "{call BBTRXGUDANG.SETAPP_ACCESS(?)}";
            call = con.prepareCall(sql);
            call.setString(1, map.get("app_access").toString());
            call.execute();
            
            sql = "{? = call BBTRXGUDANG.INSERTGUDANGBB(?,?)}";
            call = con.prepareCall(sql);
            call.registerOutParameter(1, OracleTypes.VARCHAR);
            call.setString(2, kode);
            call.setString(3, map.get("crud").toString());            
            call.execute();
            
            map.clear();
            map.put("message", call.getString(1));
        }catch (Exception e) {
			e.printStackTrace();
			map.clear();
            map.put("message", e.getMessage());  
            
		}
        lMap.add(map);
        return lMap;

	}
}
